package com.lq.javawebTest.controller;

import com.lq.JDBC.Dao.UserDao;
import com.lq.JDBC.Dao.impl.UserDaoImpl;
import com.lq.JDBC.entity.User;
import com.lq.javawebTest.core.entity.HttpRequest;
import com.lq.javawebTest.core.entity.HttpResponse;
import com.lq.javawebTest.core.utils.HttpResponseHandler;
import com.lq.javawebTest.core.inter.Servlet;

public class UserServlet implements Servlet {
    @Override
    public void service(HttpRequest request, HttpResponse response) {
        if(request.getUri().contains("/user.do")){
            String name = request.getParameter("name");
//            int i = Integer.parseInt(request.getParameter("id"));
            UserDao userDao = new UserDaoImpl();
            User user = userDao.queryByName(name, User.class);
            if(user != null){
                response.setHeader("Content-Type","text/plain;charset=UTF-8");
                response.setHeader("Content-Length",Integer.toString(user.toString().getBytes().length));
                response.setBody(user.toString());
                // 将回复报文实例先转化为String实例，然后再写入到输出流给浏览器
                HttpResponseHandler.writeResponse(response, response.getOutputStream());
        }
    }
    }
}
